:- module(get_page_info, [
        page_def/4,
        resolve_link/2
                         ], [functions, hiord]).

:- reexport(portalfiles(links), [link_info/3]).
:- reexport(portalfiles(boxes), [box_def/2]).

:- use_module(library(lists), [append/3]).

:- use_module(amos(configuration), [
        root_url/1,
        cgi_url/1
        ]).

:- use_module(page_definitions, [page_data/4]).

add_to_root(Name,
            ~append(~atom_codes(~root_url),
            ~append(~atom_codes(Name),".html"))).

add_to_cgi(Name,
           ~append(~atom_codes(~cgi_url),
           ~append(~atom_codes(Name), ".cgi"))).

root_relative(Name, ~append(~atom_codes(~root_url), Name)).

cgi_relative(Name, ~append(~cgi_url, Name)).

absolute(Name, Name).


%% page_def(PageIdentifier,
%%          PageTitle,
%%          PageURL
%%          StructureName)

page_def(PageId, PageTitle, PageURL, StructureName):-
        page_data(PageId, PageTitle, root_relative(RelURL), StructureName), !,
        root_relative(RelURL, PageURL).
page_def(PageId, PageTitle, PageURL, StructureName):-
        page_data(PageId, PageTitle, cgi_relative(RelURL), StructureName), !,
        cgi_relative(RelURL, PageURL).
page_def(PageId, PageTitle, PageURL, StructureName):-
        page_data(PageId, PageTitle, absolute(RelURL), StructureName), !,
        absolute(RelURL, PageURL).
page_def(PageId, PageTitle, PageURL, StructureName):-
        page_data(PageId, PageTitle, PageMode, StructureName), !,
        PageMode(PageId, PageURL).
 %% page_def(PageId, PageTitle, PageURL, StructureName):-
 %%         page_data(PageId, PageTitle, absolute(URL), StructureName), !,
 %%         absolute(URL, PageURL).
page_def(PageId, _PageTitle, _PageURL, _StructureName):-
        display(page_def_not_found(PageId)),
        nl.

resolve_link(link_page(PageId), RealLink):-
        page_def(PageId, _, RealLink, _),
        !.
resolve_link(link_page(PageId, Label), RealLink):-
        page_def(PageId, _, HTMLLink, _),
%        append(HTMLLink, "#"||~atom_codes(Label)),
        atom_codes(Label, LabelCodes),
        append(HTMLLink, "#"||LabelCodes, RealLink),
        !.
resolve_link(Url, Url):-
        Url = [_|_],                            % Explicit
        !.
resolve_link(Link, "Unknown link!"):-
        display(user_error, unknown_link(Link)),
        nl(user_error).
